library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.8
# Wave 2

# Create binaries for Q20DD2 - Active duty Gen/Adm to crit President
summary(factor(df$Q20DD))

df$Q20DD2[df$Q20DD < 3] <- 1
df$Q20DD2[df$Q20DD > 2 & df$Q20DD < 77] <- 0
summary(factor(df$Q20DD2))

# P_Q20 is Criticize conditions: 1 = Crit Trump; 2 = Crit Obama; 3 = Crit POTUS
summary(factor(df$P_Q20))

# Drop NA
df2 <- df[!is.na(df$Q20DD2),]
df2 <- df2[!is.na(df2$party),]

# Creating weighted survey design objects
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

# Wave 2
# Criticize Trump
w2t_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2t_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2t_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2o_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2o_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2p_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2p_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2],method='me',df=degf(w2_design))




# Wave 2
pt.est1 <- c(w2t_d[1],w2t_i[1],w2t_r[1],1,
             w2o_d[1],w2o_i[1],w2o_r[1],1,
             w2p_d[1],w2p_i[1],w2p_r[1])
ci.low1 <- c(attr(w2t_d,'ci')[1],attr(w2t_i,'ci')[1],attr(w2t_r,'ci')[1],.99,
             attr(w2o_d,'ci')[1],attr(w2o_i,'ci')[1],attr(w2o_r,'ci')[1],.99,
             attr(w2p_d,'ci')[1],attr(w2p_i,'ci')[1],attr(w2p_r,'ci')[1])
ci.high1 <- c(attr(w2t_d,'ci')[2],attr(w2t_i,'ci')[2],attr(w2t_r,'ci')[2],1.1,
              attr(w2o_d,'ci')[2],attr(w2o_i,'ci')[2],attr(w2o_r,'ci')[2],1.1,
              attr(w2p_d,'ci')[2],attr(w2p_i,'ci')[2],attr(w2p_r,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(.2,.75)+
  ylab("Respondent Agreement (%)")+
  xlab("Treatment Condition")+
  theme_bw()

